package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Project：heima-leadnews
 * @Author：fangkui
 * @Date：2021/1/22
 */
@Slf4j
@Service
@Transactional
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    @Override
    public ResponseResult findByNameAndPage(ChannelDto channelDto) {
        // 1.检查参数
        if (channelDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        channelDto.checkParam();// 检查分页查询
        // 2.封装查询条件
        Page pageRequest = new Page<>(channelDto.getPage(),channelDto.getSize()); // 分页条件
        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isBlank(channelDto.getName())) {
            // 根据名称模糊查询
            queryWrapper.like(AdChannel::getName,channelDto.getName());
        }
        // 3.分页查询
        // 参数1：分页条件 参数2：查询条件
        IPage<AdChannel> pageResult =  page(pageRequest,queryWrapper);
        // 4.封装响应结果返回
        ResponseResult responseResult = new PageResponseResult(channelDto.getPage(), channelDto.getSize(), pageResult.getTotal());
        responseResult.setData(pageResult.getRecords()); // 分页查询的结果集封装
        return responseResult;
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
        // 1.检查参数
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2.补全实体类属性
        adChannel.setCreatedTime(new Date());
        adChannel.setIsDefault(false);
        // 3.保存实体类
        save(adChannel);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        // 1.检查参数
        if (adChannel == null || adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2.执行修改
        updateById(adChannel);
        // 3.保存实体类
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        // 1.检查参数
        if (id == null) {
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2.查询参数是否存在，存在则删除，不存在则提示
        AdChannel adChannel = getById(id);
        if (adChannel == null) {
            ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道信息不存在！");
        }
//        if (adChannel.getStatus()) {
//            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道有效不能删除！");
//        }
        // 3.执行删除
        removeById(id);
//        System.out.println(1/0);
        // 自定义抛异常 commons包下创建一个自定义异常类
        if (id > 10) {
            throw new CustomException(ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"id不可以大于10"));
        }
        // 4.返回结果
        return ResponseResult.okResult();
    }
}
